home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 4.1 KB | 175 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 2.9 (Nonlinear Seidel Iteration).
- % Section 2.6, Iteration for Nonlinear Systems, Page 108
- echo on; clc; format long; hold off; clear
-
- % This program implements fixed point iteration
-
- % in 2-dimensions for solving X = G(X).
-
- % Use the vector notation X = (x y).
-
- % Define and store the functions g1(X) and g2(X) and
-
- % define and store the function G(X) in the
-
- % M-files g1.m g2.m and G.m respectively.
-
- pause % Press any key to continue.
-
- clc;
- % function z = g1(x,y)
- % z = (x.^2 - y + 0.5)./2;
-
- % function z = g2(x,y)
- % z = (- x.^2 - 4.*y.^2 + 8.*y + 4)./8;
-
- % function Z = G(X)
- % x = X(1); y = X(2);
- % Z = [g1(x,y) g2(x,y)];
-
- delete g1.m
- diary g1.m; disp('function z = g1(x,y)');...
- disp('z = (x.^2 - y + 0.5)./2;');...
- diary off;
-
- delete g2.m
- diary g2.m; disp('function z = g2(x,y)');...
- disp('z = (- x.^2 - 4.*y.^2 + 8.*y + 4)./8;');...
- diary off;
-
- delete G.m
- diary G.m; disp('function Z = G(X)');...
- disp('x = X(1); y = X(2);');...
- disp('Z = [g1(x,y) g2(x,y)];');...
- diary off;
-
- % Remark. g1.m, g2.m, G.m and fix2dim.m are used for Algorithm 2.9
- g1(0,0); g2(0,0); G([0 0]); % Test for files g1.m g2.m G.m
- pause % Press any key to graph x = g1(x,y) and y = g2(x,y).
-
- clc; clg;
- % Plot x = g1(x,y) and y = g2(x,y) over the rectangle [a,b]x[c,d].
-
- a = -2.0;
- b = 2.5;
- c = -1.0;
- d = 1.5;
- hx = (b-a)/31;
- hy = (d-c)/31;
- [X Y] = meshdom(a:hx:b, c:hy:d);...
- Z = g1(X,Y) - X;...
- W = g2(X,Y) - Y;...
- contour(Z,[0 0],a:hx:b,c:hy:d,'-g');...
- grid;...
- hold on;...
- contour(W,[0 0],a:hx:b,c:hy:d,'-g');...
- title('Implicit plot of x = g1(x,y) and y = g2(x,y).');...
- hold off;...
- shg; pause % Press any key to perform fixed point iteration.
-
- clc;
- % Place the starting value in [p0 q0]
-
- % Place the tolerance in delta
-
- % Place the number of iterations in max1
-
- p0 = 0.0;
- q0 = 1.0;
- P0 = [p0 q0];
- delta = 1e-12;
- max1 = 50;
-
- [P0,err,P] = fix2dim('G',P0,delta,max1);
-
- pause % Press any key for the list of iterations.
-
- clc; clg;
- a = -0.275;
- b = 0.025;
- c = 0.99;
- d = 1.002;
- hx = (b-a)/20;
- hy = (d-c)/20;
- [X Y] = meshdom(a:hx:b, c:hy:d);...
- Z = g1(X,Y) - X;...
- W = g2(X,Y) - Y;...
- contour(Z,[0 0],a:hx:b,c:hy:d,'-g');...
- hold on;...
- contour(W,[0 0],a:hx:b,c:hy:d,'-g');...
- X0 = P(:,1);...
- Y0 = P(:,2);...
- plot(X0,Y0,'or');...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- title('Graphical presentation of the fixed point iteration.');...
- grid;...
- hold off;...
- shg; pause % Press any key to continue.
-
- clc;
- Mx1 = 'Computations for the fixed point iteration method.';
- Mx2 = ' p(k) q(k)';
- Mx3 = 'The solution is:';
- Mx4 = 'The error estimate for P is ';
- Mx5 = num2str(err);
- Mx6 = [Mx4,'[± ',Mx5,', ± ',Mx5,']'];
- clc,echo off,diary output,...
- disp(''), disp(Mx1),disp(''), disp(Mx2),disp(P),...
- disp('Iteration is converging linearly to the root.'),...
- disp(''),disp(Mx3),disp(''),disp('G(P) = P = '),disp(P0),...
- disp(''),disp([Mx6]),diary off,echo on
-
- pause % Press any key to perform fixed point iteration.
-
- clc;
- % Place the starting value in [p0 q0]
-
- % Place the tolerance in delta
-
- % Place the number of iterations in max1
-
- p0 = 2.0;
- q0 = 0.0;
- P0 = [p0 q0];
- delta = 1e-12;
- max1 = 6;
-
- [P0,err,P] = fix2dim('G',P0,delta,max1);
-
- pause % Press any key for the list of iterations.
-
- clc; clg;
- a = -2;
- b = 10;
- c = -3;
- d = 1;
- hx = (b-a)/31;
- hy = (d-c)/31;
- [X Y] = meshdom(a:hx:b, c:hy:d);...
- Z = g1(X,Y) - X;...
- W = g2(X,Y) - Y;...
- contour(Z,[0 0],a:hx:b,c:hy:d,'-g');...
- hold on;...
- contour(W,[0 0],a:hx:b,c:hy:d,'-g');...
- X0 = P(:,1);...
- Y0 = P(:,2);...
- plot(X0,Y0,'or');...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- title('Graphical presentation of the fixed point iteration.');...
- grid;...
- hold off;...
- shg; pause % Press any key to continue.
-
- clc,echo off, diary output,...
- disp(''), disp(Mx1),disp(''), disp(Mx2),disp(P),...
- disp('Iteration is diverging to "infinity".'),...
- disp('Note the "scale factor" for the table of computations.'),...
- diary off, echo on
-